package com.sky.service.impl;

import com.alibaba.druid.sql.visitor.functions.If;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategaryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class CategaryServiceimpl implements CategaryService {
    @Autowired
    CategaryMapper categarymapper;
    @Autowired
    DishMapper dishMapper;
@Autowired
SetmealMapper setmealMapper;
    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page page = categarymapper.page(categoryPageQueryDTO);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    @Override
    public List<Category> list(Integer type) {
        return categarymapper.list(type);
    }


    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category =new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categarymapper.update(category);
    }

    @Override
    public void startend(Integer status, Long id) {
        Category category = Category.builder()
                .id(id)
                .status(status)
                .build();
        categarymapper.update(category);
    }

    @Override
    public void add(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setStatus(0);
        categarymapper.inset(category);
    }

    @Override
    public Result delete(Integer id) {
        if(dishMapper.selectca(id)!=0)
            return Result.error( MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        if(setmealMapper.selectca(id)!=0)
            return Result.error( MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        categarymapper.delete(id);
        return Result.success();
    }
}
